﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class RobSceneLoad : MonoBehaviour {

    public Text text;
    public Slider slider;
    public Image fader;
    public GameObject other;
    public Animator animator;
    string targetSceneName;

    private void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    public string TargetSceneName
    {
        set
        {
            targetSceneName = value;
            StartCoroutine(LoadScene(targetSceneName));
            //暗
            animator.Play("LoadAnimFadeIn");
        }
    }

    AsyncOperation asyncOperation;
 
    IEnumerator LoadScene(string sceneName)
    {
        yield return new WaitForSeconds(1.2f);
        asyncOperation = SceneManager.LoadSceneAsync(sceneName);


        while (!asyncOperation.isDone)
        {
        
            float p = asyncOperation.progress * 100 + 10f;
            if (p > 100) p = 100;
            text.text =  p + "%";

            slider.value = Mathf.Lerp(slider.value, asyncOperation.progress, Time.time);

            yield return null;
        }

        while (asyncOperation.isDone)
        {

           
            slider.value= 1;

            Invoke("FadeOut", 2);
            Invoke("Destory", 3f);
            break;
        }

    }

    void FadeOut()
    {
        //亮
        other.SetActive(false);
        animator.Play("LoadAnimFadeOut");
       
    }

    void Destory()
    {
       
        DestroyObject(gameObject);
    }

 
}
